/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import cacao.client.CaCaO;
import cacao.client.data.CaCaOHelper;
import cacao.client.data.CatalogObject;
import cacao.client.data.ItemDetailsObject;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;


public class ImageCmp extends Composite implements CaCaOComposite{
	public static final int ZOOM_100 = 1;
	public static final int ZOOM_HEIGHT = 2;
	public static final int ZOOM_WIDTH = 3;
	public static final int ZOOM_PAGE = 4;
	public static final int ZOOM_IN = 5;
	public static final int ZOOM_OUT = 6;

	private ScrollPanel scrollPanel;
	private Image image;
	private CaCaO application;
	private TabLayoutPanel dp;
	private Frame catalogInfo;

	public ImageCmp(CaCaO app){
		application = app;
		dp = new TabLayoutPanel(0, Unit.PX);
		dp.setHeight("100%");
		scrollPanel = new ScrollPanel();
		dp.add(scrollPanel);
		image = new Image();
		image.setUrl("");

		scrollPanel.add(image);

		catalogInfo = new Frame();

		dp.add(catalogInfo);

		catalogInfo.setHeight("99%");
		catalogInfo.setWidth("99.5%");

		initWidget(dp);
	}



	public void zoomImage(int mode){
		if(mode == ZOOM_100){
			image.setWidth("");
			image.setHeight("");
		}
		if(mode == ZOOM_WIDTH){
			image.setWidth((dp.getOffsetWidth()-20)+"px"); //"100%" does not work in standard mode
			image.setHeight("");
		}
		if(mode == ZOOM_HEIGHT){
			image.setWidth("");
			image.setHeight((dp.getOffsetHeight()-20)+"px");  //"100%" does not work in standard mode
		}
		if(mode == ZOOM_IN){
			image.setWidth(Integer.toString(Math.round((float)(image.getWidth()*1.25)))+"px");
			image.setHeight("");
		}
		if(mode == ZOOM_OUT){
			image.setWidth(Integer.toString(Math.round((float)(image.getWidth()*0.8)))+"px");
			image.setHeight("");
		}
		if(mode == ZOOM_PAGE){
			image.setWidth("100%");
			image.setHeight("");
			if(((float)image.getWidth()/scrollPanel.getOffsetWidth()+1)<((float)image.getHeight()+1/scrollPanel.getOffsetHeight()+1)){
				zoomImage(ZOOM_HEIGHT);
			}
			else{
				zoomImage(ZOOM_WIDTH);
			}
		}
		updateUI();
	}

	public void updateUI(){
		ItemDetailsObject currItem = application.getCurrentItem();
		if(currItem!=null){
			int lastChunkLength = Integer.parseInt(application.getProperties().get("CaCaO.client.file.lastchunklength").trim());
			dp.selectTab(0);
			boolean debug = "true".equals(application.getProperties().get("CaCaO.client.debug"));
			image.setUrl(application.getProperties().get("CaCaO.client.file.url")+CaCaOHelper.generatePath(currItem.getId(), lastChunkLength, application.getCurrentCatalog().getExtension(), debug));
			if(application.isRezoom()){
				//ToDo use zoomImage() function
				image.setWidth("");
				image.setHeight((dp.getOffsetHeight()-20)+"px");  //"100%" does not work in standard mode
				application.setRezoom(false);
			}
		}
		else{
			dp.selectTab(1);
			CatalogObject curCat = application.getCurrentCatalog();
			if(curCat!=null){
				catalogInfo.setUrl("frontpages/frontpage_"+curCat.getCode()+".html");
			}			
		}
	}
}
